package persistence;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.stat.*;

import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil
{
	private static SessionFactory sessionFactory;
	
	static
	{
		try
		{
			//sessionFactory=new AnnotationConfiguration().configure("hibernate_hsql.cfg.xml").buildSessionFactory();
			sessionFactory=new AnnotationConfiguration().configure("hibernate_mysql.cfg.xml").buildSessionFactory();
			
			Statistics stats=sessionFactory.getStatistics();
			stats.setStatisticsEnabled(true);
		}catch(Throwable ex)
		{
			throw new ExceptionInInitializerError(ex);
		}
	}
	
	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}
	
	public static void shutdown()
	{
		//close caches and connection pools
		getSessionFactory().close();
	}
	
	public static void stat()
	{
		Statistics stats=sessionFactory.getStatistics();
		stats.logSummary();
		
		EntityStatistics msgStats=stats.getEntityStatistics("hello.Message");
		System.out.println("hello.Message fetch count--"+msgStats.getFetchCount());
	}
}